package cc.zy.base.hr.service.impl;

import cc.zy.base.businesses.mapper.BatchMapper;
import cc.zy.base.businesses.mapper.DicMapper;
import cc.zy.base.businesses.mapper.LevelMapper;
import cc.zy.base.businesses.mapper.MajorMapper;
import cc.zy.base.common.entity.FebsConstant;
import cc.zy.base.common.entity.QueryRequest;
import cc.zy.base.common.utils.SortUtil;
import cc.zy.base.hr.entity.Emolument;
import cc.zy.base.hr.mapper.EmolumentMapper;
import cc.zy.base.hr.service.IEmolumentService;
import com.baomidou.mybatisplus.core.conditions.query.LambdaQueryWrapper;
import com.baomidou.mybatisplus.core.metadata.IPage;
import com.baomidou.mybatisplus.core.toolkit.CollectionUtils;
import com.baomidou.mybatisplus.extension.plugins.pagination.Page;
import com.baomidou.mybatisplus.extension.service.impl.ServiceImpl;
import lombok.RequiredArgsConstructor;
import org.springframework.stereotype.Service;
import org.springframework.transaction.annotation.Propagation;
import org.springframework.transaction.annotation.Transactional;

import javax.annotation.Resource;
import java.util.Arrays;
import java.util.List;

/**
 * 院校
 * Service
 *
 * @author Jiangjinlin
 * @date 2021-01-18 10:51:13
 */
@Service
@RequiredArgsConstructor
@Transactional(propagation = Propagation.SUPPORTS, readOnly = true)
public class EmolumentServiceImpl extends ServiceImpl<EmolumentMapper, Emolument> implements IEmolumentService {

    @Resource
    private final EmolumentMapper emolumentMapper;
    private final LevelMapper levelMapper;
    private final MajorMapper majorMapper;
    private final BatchMapper batchMapper;
    private final DicMapper dicMapper;

    @Override
    public IPage<Emolument> findEmoluments(QueryRequest request, Emolument emolument) {
        Page<Emolument> page = new Page<>(request.getPageNum(), request.getPageSize());
        page.setSearchCount(false);
        page.setTotal(baseMapper.countEmolumentDetail(emolument));
        SortUtil.handlePageSort(request, page, "id", FebsConstant.ORDER_ASC, false);
        return baseMapper.findEmolumentDetailPage(page, emolument);
    }

    @Override
    public Emolument findById(Integer id) {
        return baseMapper.findById(id);
    }

    @Override
    @Transactional(rollbackFor = Exception.class)
    public void deleteEmoluments(String[] emolumentIds) {
        List<String> list = Arrays.asList(emolumentIds);
        // 删除用户
        this.removeByIds(list);
    }

    @Override
    public List<Emolument> findEmolumentListNotPage(Emolument emolument) {
        List<Emolument> emolumentListNotPage = baseMapper.findEmolumentNotPage(emolument);
        return emolumentListNotPage;
    }

    @Override
    public Emolument findEmolumentDetailList(Integer id) {
        Emolument param = new Emolument();
        param.setId(id);
        List<Emolument> emolument = baseMapper.findEmolumentDetail(param);
        return CollectionUtils.isNotEmpty(emolument) ? emolument.get(0) : null;
    }

    @Override
    public List<Emolument> findEmoluments(Emolument emolument) {
        LambdaQueryWrapper<Emolument> queryWrapper = new LambdaQueryWrapper<>();

        return baseMapper.selectList(queryWrapper);
    }

    @Override
    @Transactional(rollbackFor = Exception.class)
    public void createEmolument(Emolument emolument) {

        this.save(emolument);
    }

    @Override
    @Transactional(rollbackFor = Exception.class)
    public void updateEmolument(Emolument emolument) {
        this.saveOrUpdate(emolument);
    }

    @Override
    @Transactional(rollbackFor = Exception.class)
    public void deleteEmolument(Emolument emolument) {
        LambdaQueryWrapper<Emolument> wrapper = new LambdaQueryWrapper<>();

        this.remove(wrapper);
    }

    /**
     * 方法描述：方法描述：查询所有院校[id、名称]
     *
     * @return java.util.List<cc.zy.base.businesses.entity.College> 院校集合
     * @author LiPeng
     * @date 2021/3/9
     */
//    @Override
//    public List<College> findCollegeForSelect() {
//
//        return collegeMapper.selectCollegeForSelect();
//    }
//
//    /**
//     * 方法描述：根据院校id查询层次[id、名称]，正序
//     *
//     * @param collegeId 院校id【必传项】
//     * @return java.util.List<cc.zy.base.businesses.entity.Level> 层次集合
//     * @author LiPeng
//     * @date 2021/3/9
//     */
//    @Override
//    public List<Level> findLevelForSelect(Integer collegeId) {
//
//        return levelMapper.selectLevelForSelect(collegeId);
//    }
//
//    /**
//     * 方法描述：根据院校id、层次id查询专业[id、名称]
//     *
//     * @param collegeId 院校id【必传项】
//     * @param levelId   层次id【必传项】
//     * @return java.util.List<cc.zy.base.businesses.entity.Major> 专业集合
//     * @author LiPeng
//     * @date 2021/3/9
//     */
//    @Override
//    public List<Major> findMajorForSelect(Integer collegeId, Integer levelId) {
//
//        return majorMapper.selectMajorForSelect(collegeId, levelId);
//    }
//
//    /**
//     * 方法描述：获取所有有效批次[id、名称]，倒序
//     *
//     * @return java.util.List<cc.zy.base.businesses.entity.Batch> 批次集合
//     * @author LiPeng
//     * @date 2021/3/6
//     */
//    @Override
//    public List<Batch> selectBatchForWx() {
//
//        return batchMapper.selectBatchForWx();
//    }
//
//    /**
//     * 方法描述：根据学生信息获取当前学生有效批次[id、名称]，倒序
//     *
//     * @return java.util.List<cc.zy.base.businesses.entity.Batch> 批次集合
//     * @author JIANGJINLIN
//     * @date 2021/3/6
//     */
//    @Override
//    public List<Batch> selectBatchByStuForWx(String studentName, String identity) {
//
//        return batchMapper.selectBatchByStuForWx(studentName, identity);
//    }
//
//    /**
//     * 方法描述：根据学生信息获取当前学生有效批次[id、名称]，倒序不包含当前登录信息
//     *
//     * @return java.util.List<cc.zy.base.businesses.entity.Batch> 批次集合
//     * @author JIANGJINLIN
//     * @date 2021/3/6
//     */
//    @Override
//    public List<Batch> selectBatchByStuButThisForWx(String studentName, String identity, String batchName, String studyTypeName) {
//
//        return batchMapper.selectBatchByStuButThisForWx(studentName, identity, batchName, studyTypeName);
//    }
//
//    /**
//     * 方法描述：获取所有有效批次[id、名称]，倒序
//     *
//     * @return java.util.List<cc.zy.base.businesses.entity.Batch> 批次集合
//     * @author LiPeng
//     * @date 2021/3/6
//     */
//    @Override
//    public List<Batch> findBatchForSelect() {
//
//        return batchMapper.selectBatchForSelect();
//    }
//
    @Override
    public Boolean isRepetitive(String name, String simpleName) {
        List<Emolument> repetitive = emolumentMapper.isRepetitive(name, simpleName);
        Boolean flag = true;
        if (repetitive != null && repetitive.size() > 0) {
            flag = false;
        }
        return flag;
    }
//
//    /**
//     * 方法描述：查询所有学习形式
//     *
//     * @return java.util.List<cc.zy.base.businesses.entity.Dic> 学习形式集合
//     * @author LiPeng
//     * @date 2021/3/9
//     */
//    @Override
//    public List<Dic> findStudyTypeForSelect() {
//
//        return dicMapper.findDicByType("study_type");
//    }
//
//    /**
//     * 方法描述：查询校区
//     *
//     * @return java.util.List<cc.zy.base.businesses.entity.Dic> 校区集合
//     * @author LiPeng
//     * @date 2021/3/18
//     */
//    @Override
//    public List<Dic> findCampusForSelect() {
//
//        return dicMapper.findDicByType("campus");
//    }
}